Chapter 12: Importing external modules

Most of the functionality we have used thus far, is simply built into the Python language itself. Now we have also learnt how to write your own functions. Often however you need to use external modules in your code. A lot of external modules are already available in the Python Standard Library, for a wide variety of tasks. There are also countless third-party providers of Python modules.

At the end of this chapter, you will be able to:

  • import an entire module
  • import a specific function from a module
  • use dir and help to get information about a module or function
  • import a few commonly used modules: random, datetime, and requests

If you want to learn more about these topics, you might find the following links useful:

Acknowledgements:

We use some materials from this other Python course.

If you have questions about this chapter, please refer to the forum on Canvas.

1. Importing a module or a function

Python modules

A python module is a python file (i.e. a file ending in .py) which contains function definitions and statements. You can already write one yourself! (More about this in the next chapter.)

Some modules are part of the python standard library and contain useful functions. You can simply import these mudules and use the functions as explained below.

More on modules can be found here: https://docs.python.org/2/tutorial/modules.html

To use an external module in your code you need to explicitly import it. Consider for example the module random from the standard library, which contains functions for generating random numbers:


In [ ]:
import random
print(random.randint(0, 100))

Note the syntax used: using the dot, we indicate that our machine should look for the randint() method inside the random module we just imported. You can import an entire module or import only a specific function in the module. We could also have imported the (single) function we needed as follows:


In [ ]:
from random import randint
print(randint(0, 100))

In this case we wouldn't have to specify where or machine should find the randint() function: note that we don't need to use the module random in the function call in this case.

2. Getting help

We might not understand how an external method works, or what are the inputs it needs, or what are the output values it returns. In that case, we can use help() (just as we did with functions and methods):


In [ ]:
help(randint)

If we like the randint() method, we might also be interested which other methods are offered in the random package. We can use dir() for this:


In [ ]:
dir(random)

You can also (temporarily) change the names of the functions you import:


In [ ]:
from random import randint as random_number
print(random_number(0, 100))

If you want to import the entire module, it works the same (using the syntax we have already seen above). Chanding the name can be useful if we want to shorten it (as shown below).


In [ ]:
import random as rn
print(rn.randint(0,4))

3. Other useful modules

There are plenty of very useful modules out there, so it is a good practice to check if someone has already created a module for a task that you are facing. Here, we'll show

  • datetime (module to manipulate dates)
  • requests (module to download the content of a webpage)

3.1 Datetime


In [ ]:
import datetime

print(datetime.datetime.now())

We can decipher the output ourselves: the first value is the current year, the second the current month, then day, hour, minute, etc. We can also see what Python tells us about this result:


In [ ]:
help(datetime.datetime.now)

3.2 Requests


In [ ]:
import requests
a=requests.get("https://piasommerauersite.wordpress.com/")
print(a.content)

Don't worry too much about the response: it is a format called HTML (which is basically behind all web pages) and one would need to write some more code to extract the useful information from it. But: it is really impressive that we can retrieve any webpage from the web with just three lines of code! If you want to have a quick look at how to extract content, feel free to check out the beautifulsoup module.

So, how does one find a useful module for a specific task? Answer: G00gle it ;-)

Exercises

Exercise 1:

Check which arguments should be supplied to the random.sample() method and what is their intention. Then call this function with different arguments.


In [ ]:
# your code here

Exercise 2

Figure out what the os module does and think about tasks it could be used for.


In [ ]:
# play around here